package com.shuwen.gcdj.service.file;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.fastjson.JSONArray;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.exception.ApiException;
import com.shuwen.gcdj.common.util.file.ExcelFileUtil;
import com.shuwen.gcdj.common.util.file.ExcelListener;
import com.shuwen.gcdj.common.util.file.FileUploadUtil;
import com.shuwen.gcdj.common.util.file.SaveInterface;
import com.shuwen.gcdj.dto.file.ExcelStateDataDto;
import com.shuwen.gcdj.entity.TestBean;
import com.shuwen.gcdj.mapper.mysql.TestUserMapper;
import com.shuwen.gcdj.service.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/***
 ** Created by ljf
 ** Date:2023/3/13
 */
@Service
@Slf4j
public class ExcelService implements  SaveInterface<TestBean> {

    @Autowired
    private RedisService redisService;

    @Autowired
    private TestUserMapper testUserMapper;

    @Autowired
    private ExcelStateDataDto excelStateDataDto;

    @Autowired
    private Environment environment;

    @Value("${repairspath}")
    private String fileName;



    public void importExcel() {
        String string = redisService.getString("11111");
        List<TestBean> testBeans = (List<TestBean>) JSONArray.parse(string);
        testUserMapper.insertBatch(testBeans);
        redisService.delete("11111");
    }



    public void exportExcel(HttpServletResponse response) {
        List<TestBean> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            TestBean bean = new TestBean();
            bean.setExport("出口啊");
            bean.setTime("2023-03-17");
            bean.setDirection("上海"+i);
            bean.setData1(1.0);
            bean.setData2(2.0);
            bean.setData3(3.0);
            bean.setData4(3.0);
            bean.setData5(5.0);
            bean.setData6(5.0);
            list.add(bean);
        }
        ExcelFileUtil.exportEasyExcel(list,response);
    }


    public String uploadFile(MultipartFile file,Boolean isTrue,String pathNow) {
        String path=null;
        String savePath=null;
        try {
            if (file == null) {
                throw new ApiException("文件不能为空");
            }
            if (isTrue){
            //得到上传的保存路径‘
             savePath = pathNow;
            }else {
             savePath = fileName;
            }
            //创建目录
            FileUploadUtil.mkdir(savePath);
            //得到上传的原文件名
            String filename = FileUploadUtil.getMaxFilename(savePath) + FileUploadUtil.getFileSuffix(file.getOriginalFilename());
            if (excelStateDataDto.getErrorCount()>0){
               throw new ApiException("文件上传失败，请重新检查！！！！！！！");
            }
            //保存
            path  = FileUploadUtil.doPath(filename, savePath, file);
        }catch (Exception e){
            System.out.println(e);
        }
        return path;
    }

    public CommonResult previewFile(MultipartFile file) {
        log.info("importExcel.request[{}]",file.getOriginalFilename());
        if (file.getOriginalFilename() == null || !file.getOriginalFilename().endsWith("xlsx")) {
            return CommonResult.failed("文件类型不对");
        }
        try {
            //headRowNumber 2 代表从第3行开始导入 第一行为大标题 第二行为表头
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), TestBean.class,
                    new ExcelListener(this, excelStateDataDto));
            read.headRowNumber(1).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success(excelStateDataDto);
    }

    @Override
    public void save(List<TestBean> list, Map<Integer, Object> param) {
        testUserMapper.insertBatch(list);
    }
}
